package mvp.com.bbc.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import java.util.List;

import mvp.com.bbc.R;

/**
 * Created by Administrator on 2017/10/7.
 */

public abstract class ArtListFragment2<VH extends RecyclerView.ViewHolder> extends Fragment {

    public View mView;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRV;
    private int pager;
    private boolean loading = true;
    private BaseFragmentAdapter mAdapter;
    public LayoutInflater mInflater;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.base_fragment_ref, container, false);
        Log.v("cbf", "onCreateView===");
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.v("cbf", "onActivityCreated===");
        mRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.refreshlayou);
        mRV = (RecyclerView) mView.findViewById(R.id.base_rv);
        mInflater = LayoutInflater.from(getActivity());
        //设置下拉时状态的颜色
        mRefreshLayout.setColorSchemeResources(R.color.indianred, R.color.peru, R.color.violet);
        //刷新最新数据
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData(0);
                pager = 0;
                loading = false;
            }
        });
        if (mRV==null){
            if (mRV.getChildCount()>0){
                if (mRV.getChildAt(0) instanceof RecyclerView){
                    mRV = (RecyclerView) mRV.getChildAt(0);
                    //设置RecyclerView的监听
                    setListViewOnScroll();
                }
            }
        }
        mRV.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new BaseFragmentAdapter();
        mRV.setAdapter(mAdapter);
        mRV.addOnScrollListener(new BaseFragmentOnScrollListener());
        initData(0);
        pager = 0;
    }

    private void setListViewOnScroll() {
        mRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                // 移动过程中判断时候能下拉加载更多
                if (canLoadMore()) {
                    // 加载数据
                    loadData();
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    private void loadData() {

    }

    private boolean canLoadMore() {
        return false;
    }

    class BaseFragmentAdapter extends RecyclerView.Adapter<VH> {
        private List data;

        public void setData(List<?> data) {
            if (data != null) {
                this.data = data;
                notifyDataSetChanged();
            }
        }

        public void addData(List<?> data) {
            if (data!=null){
                this.data.addAll(data);
                notifyDataSetChanged();
            }
        }

        @Override
        public VH onCreateViewHolder(ViewGroup parent, int viewType) {
            return initHolderView(parent);
        }

        @Override
        public void onBindViewHolder(VH holder, int position) {
            initHolderData(holder, position);
            initHolderSetListener(holder, holder.getLayoutPosition());
        }

        @Override
        public int getItemCount() {
            if (data != null) {
                return data.size();
            }
            return 0;
        }
    }

    public class BaseFragmentOnScrollListener extends RecyclerView.OnScrollListener {
        private int previousTotal = 0;
        int firstVisibleItem, visibleItemCount, totalItemCount, layoutManagerType = -1;

        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                firstVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            } else if (layoutManager instanceof GridLayoutManager) {
                firstVisibleItem = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            } else {
                throw new RuntimeException(
                        "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
            }
            visibleItemCount = recyclerView.getChildCount();
            totalItemCount = layoutManager.getItemCount();

            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                }
            }
            if (!loading && (totalItemCount - visibleItemCount) <= firstVisibleItem) {
                pager++;
                initData(pager);
                loading = true;
            }
        }

    }

    /**
     * 通知更新
     */
    public void update(List<?> data) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        mAdapter.setData(data);
    }
    public void addData(List<?> data){
    }

    //初始化布局  由于父类不知道要进行什么操作 抽象出去给子类实现  让其子类来完成想要的操作
    protected abstract VH initHolderView(ViewGroup parent);

    protected abstract void initData(int pager);

    protected abstract void initHolderSetListener(VH holder, int layoutPosition);

    protected abstract void initHolderData(VH holder, int position);
}
